/**
 * 执行播放
 * @param _buyCourseId 购买的课程ID
 * @param _playCourseId 播放的课程ID
 * @param catalogId 目录ID
 * @param callback 回调
 */
function goPlay(_buyCourseId,_playCourseId,catalogId,callback) {
    var prevCatalogId = getPrevCatalogId(catalogId);
    Zepto.ajax({
        url: baseloaction +'/mobile/uc/play/checkPlay',//验证播放
        type:'post',
        dataType:'json',
        data:{
            'buyCourseId' : _buyCourseId,
            'courseId' : _playCourseId,
            'prevCatalogId' : prevCatalogId,
            'catalogId' : catalogId == null ? 0 : catalogId
        },
        success:function (result) {
            if(result.success==true){
                var entity = result.entity;
                if(entity.play==true || entity.audition ==true){
                    if(entity.isEvaluate){
                        $('#c-comment-btn').addClass('active');
                    }else{
                        $('#c-comment-btn').data('eval','已经评论过了');
                    }
                    //加载播放器
                    saveCourseLearnFlow(1);
                    var sign = entity.sign;
                    loadPlayData(sign,entity.typeKey,callback);
                    //因苹果系统兼容性问题,流量提醒设置暂时注销
                    //readyPlay(result,callback);
                }else{
                    if(entity.needBuy!=undefined&&entity.needBuy!=null&&entity.needBuy==1||entity.needBuy==2){
                        guideBuyCourse(result.message,courseId);
                    }else{
                        Zepto.toast(result.message);
                    }


                }
            }else{
                Zepto.toast(result.message);
            }
        },
        error:function (error) {
            Zepto.toast('系统错误，课程节点学习/播放失败');
        }
    });
}

/**
 * 播放前验证当前网络环境
 * @param entity
 * @param typeKey
 */
function readyPlay(result,callback) {
    var entity = result.entity;
    var sign = entity.sign;
    var wifi = result.wifi;
    var typeKey = entity.typeKey;

    var connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection || {type: 'unknow', effectiveType: 'unknow', downlink: 'unknow'};
    var type = (connection.type || connection.effectiveType || 'unknow');

    if (type != 'wifi') {
        if (wifi == 0){// 当前网络不能确定为wifi且未设置仅wifi下观看时
            if (typeKey == 'VIDEO' || typeKey == 'AUDIO'){
                Zepto.confirm('移动网络下可能会消耗大量流量，请注意。确认继续?', function () {
                    loadPlayData(sign,entity.typeKey,callback);
                })
            }else{
                loadPlayData(sign,entity.typeKey,callback);
            }
        }
        if (wifi==1){// 当前网络不能确定为wifi且设置仅wifi下观看时
            if (typeKey == 'VIDEO' || typeKey == 'AUDIO') {
                Zepto.confirm('你已开启仅在wifi下观看。是否继续观看？', function () {
                    loadPlayData(sign,entity.typeKey,callback);
                });
            }else{
                loadPlayData(sign,entity.typeKey,callback);
            }
        }
    }
    // 当前网络确定为wifi时
    if (type == 'wifi') {
        loadPlayData(sign,entity.typeKey,callback);
    }
}

/**
 * 获取前一个节点目录的ID
 * @param catalogId 当前获取目录的ID
 */
function getPrevCatalogId(catalogId) {
    var allCatalogList = Zepto("#course-catalog-list a[data-catalog]");
    var _index = -1;
    Zepto.each(allCatalogList,function (index,em) {
        var _id = Zepto(em).data("catalog");
        if(_id == catalogId){
            _index = index;
        }
    });
    if(_index > 0){
        var prev = allCatalogList[_index-1];
        return Zepto(prev).data('catalog');
    }
    return 0;
}